package com.example.iotest.bio;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class CloneTest implements Cloneable {
    @Override
    protected Object clone() throws CloneNotSupportedException {
        CloneTest clone = (CloneTest)super.clone();
        if (son == null)
            return clone;
        else {
            clone.son=(Son) clone.getSon().clone();
            return clone;
        }
    }

    public String name;
    public Son son;

    public static void main(String[] args) throws CloneNotSupportedException {
        CloneTest cloneTest = new CloneTest();
        cloneTest.setName("you").setSon(new Son().setName("er"));
        CloneTest clone = (CloneTest) cloneTest.clone();
//        clone.setSon(new Son().setName("er2"));
        clone.getSon().setName("er2");
        System.out.println(cloneTest == clone);
        System.out.println(cloneTest.son == clone.son);
        System.out.println(cloneTest.son.getName());
    }
}

@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain = true)
class Son implements Cloneable{
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    String name;
}
